package jp.co.ledinge.syllabarygame;

import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SyllabaryTableActivity extends Activity implements
		OnItemClickListener, OnCompletionListener {
	private static final String TAG = "SyllabaryTableActivity";
	private String[] syllabary;
	private int position = -1;
	private View target;
	private int[] soundId;
	private int[] imageId;
	private LinearLayout screen;
	private ImageView image;
	private MediaPlayer player; // メディアプレイヤ
	private boolean playflg = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadApps();
		setContentView(R.layout.syllabarytable);
		GridView grid = (GridView) findViewById(R.id.SyllabaryTable);
		SyllabaryAdapter adapter = new SyllabaryAdapter();
		screen = (LinearLayout) findViewById(R.id.screen);
		screen.setBackgroundColor(Color.argb(127, 255, 255, 255));
		image = (ImageView) findViewById(R.id.image);
		grid.setAdapter(adapter);
		grid.setOnItemClickListener(this);
	}

	/**
	 * 
	 */
	private void loadApps() {
		syllabary = getResources().getStringArray(R.array.raw_string_syllabary);
		TypedArray array = getResources().obtainTypedArray(
				R.array.raw_ids_sound);
		soundId = new int[array.length()];
		for (int i = 0; i < soundId.length; i++) {
			soundId[i] = array.getResourceId(i, 0);
		}
		array.recycle();
		TypedArray imagearray = getResources().obtainTypedArray(
				R.array.raw_ids_image);
		imageId = new int[imagearray.length()];
		for (int i = 0; i < imageId.length; i++) {
			imageId[i] = imagearray.getResourceId(i, 0);
		}
		imagearray.recycle();
	}

	public class SyllabaryAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return syllabary.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return syllabary[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int point, View convertView, ViewGroup parent) {
			RelativeLayout layout;
			TextView frontImage = new TextView(getApplicationContext());
			layout = new RelativeLayout(getApplicationContext());
			if (syllabary[point].length() > 0) {
				frontImage = new TextView(getApplicationContext());
				frontImage.setTextSize(36.0f);
				frontImage.setTextSize(72.0f);
				frontImage.setTextColor(Color.BLACK);
				frontImage.setText(syllabary[point]);
				frontImage.setPadding(8, 0, 5, 6);
				frontImage.setBackgroundResource(R.drawable.backimage);
				frontImage.setGravity(Gravity.CENTER);
				layout.setGravity(Gravity.CENTER);
				layout.addView(frontImage);
			}
			layout.setId(point);
			return layout;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, final int point,
			long id) {
		// TODO Auto-generated method stub
		if (!playflg) {
			view.setBackgroundColor(Color.RED);
			if (target != null) {
				target.setBackgroundColor(0);
			}
			this.position = point;
			view.setBackgroundColor(Color.RED);
			target = view;
			playSound();
		}
	}

	// サウンド再生
	public void playSound() {
		try {
			if (!playflg) {
				player = new MediaPlayer();
				player = MediaPlayer.create(this, soundId[position]);
				if (player != null) {
					player.start();
				}
				player.setOnCompletionListener(this);
				playflg = true;
				if (imageId.length > position) {
					image.setImageResource(imageId[position]);
					image.setVisibility(View.VISIBLE);
				}
				screen.setVisibility(View.VISIBLE);

			}
		} catch (Exception e) {
		}
	}

	// サウンドストップ
	public void stopSound() {
		try {
			playflg = false;
			screen.setVisibility(View.INVISIBLE);
			image.setVisibility(View.INVISIBLE);
			player.stop();
			player.setOnCompletionListener(null);
			player.release();
			player = null;
		} catch (Exception e) {
		}
	}

	// サウンド再生終了後
	public void onCompletion(MediaPlayer mediaPlayer) {
		playflg = false;
		player.setOnCompletionListener(null);
		player.release();
		player = null;
		screen.setVisibility(View.INVISIBLE);
		image.setVisibility(View.INVISIBLE);
	}

}
